function useDeal() {
  var idBar = $('#idBar').val();
  $.ajax({
      type: 'POST',
      url: '/useDeal',
      data: {idBar: idBar},
      success: function(data) {
          var JsonObj = $.parseJSON(data);
          if (JsonObj['error']) {
            if (JsonObj['error'] == 'notConnected') {
              showMessage('#welcomeProfile', JsonObj)
              swipeToPage('profile', findPageNum('profile'));
            } else if (JsonObj['error'] == 'noDealsBought') {
              showMessage('#getACouponBlocMessage', JsonObj)
              swipeToPage('profile', findPageNum('profile'));
            }
          } else {
            updateViews(JsonObj['data']);
          }
      } 
  });
}


function updateBarCuponsView(data) {
  $('#dealsUsedCurrentBarView').html('<br/> Coupon in current used : ' + data['dealsUsedCurrentBarCount']+ '<br/>');
  var cupon = $('<div></div>').addClass('cuponTemplate');
  cupon.html(buildCuponContent());
  for (var  i = 0; i < data['dealsUsedCurrentBarCount']; i++) {
    $('#dealsUsedCurrentBarView').append(cupon.clone(true));
  }
}

function buildCuponContent() {
  var content = 'Cupon<br/> '
      + 'Bar : ' + $('#titleBar').text() + '<br/>'
      + 'Utilisateur: ' + currentUserName + '<br/>';
  return content;
}

/**
 * Class to create a beer button.
 */	
function BeerButton2() {
  this.height;
  this.width;
  this.id;
  this.emptyBeer;
  this.enableFill = true;
  this.enableDrain = true;
  
  this.initialize = function(id) {
    this.id = id;
    var buttonBloc = $('#' + id);
    $('#' + id).width(this.width).height(this.height);
    buttonBloc.append($('<div></div>').width(this.width).height(this.height).attr('id', 'fullBeer'));
    buttonBloc.append($('<div></div>').width(this.width).height(this.height).attr('id', 'emptyBeer'));
    this.emptyBeer = $('#' + this.id + ' #emptyBeer');

    var isTouch = isTouchDevice();
    buttonBloc.bind( isTouch ? 'touchstart' : 'mousedown',
        this.handleMouseOver.bind(this));
    buttonBloc.bind(isTouch ? 'touchend' : 'mouseup',
        this.handleMouseOut.bind(this));
  }
  
  this.handleMouseOver = function() {
    this.emptyBeer.height(0);
    setTimeout(this.checkEndBeer.bind(this), 2100);
  }

  this.checkEndBeer = function () {
    if (this.emptyBeer.height() == 0 && this.enableDrain) {
      useDeal();
      this.enableDrain = false;
      setTimeout(this.resetBeer.bind(this), 3000);
    }
  }
  
  this.resetBeer = function() {
    this.enableDrain = true;
    this.emptyBeer.height(this.height);
  }
  
  this.handleMouseOut = function() {
    if (this.enableDrain) {
      this.emptyBeer.height(this.height);
    }
  }
}

/**
 * Class to create a beer button.
 */
function BeerButton() {
  this.currentHeight;
  this.height;
  this.width;
  this.id;
  this.emptyBeer;
  this.enableFill = true;
  this.enableDrain = false;
  this.endingTime = false;
  this.intervall = 5;
  this.step = 1;
  this.fillTime = 1000;
  
  this.initialize = function(id) {
    this.id = id;
    var buttonBloc = $('#' + id);
    $('#' + id).width(this.width).height(this.height);
    buttonBloc.append($('<div></div>').width(this.width).height(this.height).attr('id', 'fullBeer'));
    buttonBloc.append($('<div></div>').width(this.width).height(this.height).attr('id', 'emptyBeer'));
    this.emptyBeer = $('#' + this.id + ' #emptyBeer');

    this.intervall = Math.round(this.fillTime / this.height) * this.step;
    this.currentHeight = this.height;
    var isTouch = isTouchDevice();
    buttonBloc.bind( isTouch ? 'touchstart' : 'mousedown',
        this.handleMouseOver.bind(this));
    buttonBloc.bind(isTouch ? 'touchend' : 'mouseup',
        this.handleMouseOut.bind(this));
  }
  
  this.handleMouseOver = function() {
    this.enableFill = true;
    this.enableDrain = false;
    this.fillBeer();
  }

  this.handleMouseOut = function() {
    this.enableFill = false;
    this.enableDrain = true;
    this.drainBeer();
  }
  
  this.fillBeer = function() {
    if (this.currentHeight > 0 && this.enableFill) {
      setTimeout(this.fillBeer.bind(this), this.intervall);
      this.currentHeight = this.currentHeight - this.step;
      this.emptyBeer.height(this.currentHeight);
    } else if (this.currentHeight == 0 && !this.endingTime) {
      this.endingTime = true;
      useDeal();
    }
  }
  
  this.drainBeer = function() {
    if (this.emptyBeer.height() < this.height && this.enableDrain
        && !this.endingTime) {
      setTimeout(this.drainBeer.bind(this), this.intervall);
      this.currentHeight = this.currentHeight + this.step;
      this.emptyBeer.height(this.currentHeight);
    }
  } 
}
